import { message } from "@/utils/message";
import { queryStudentRouterList, updateStudentRouterStatus } from "@/api/exam";
import { ElMessageBox } from "element-plus";
import { usePublicHooks } from "../../hooks";
import { ref, onMounted } from "vue";

export function useRole() {
  const dataList = ref([]);
  const loading = ref(true);
  const switchLoadMap = ref({});
  const { switchStyle } = usePublicHooks();

  const columns: TableColumnList = [
    {
      label: "菜单编号",
      prop: "id",
      minWidth: 60
    },
    {
      label: "菜单名称",
      prop: "title",
      minWidth: 100
    },
    {
      label: "菜单路径",
      prop: "path",
      minWidth: 140
    },
    {
      label: "菜单状态",
      prop: "del",
      minWidth: 60,
      cellRenderer: scope => (
        <el-switch
          size={scope.props.size === "small" ? "small" : "default"}
          loading={switchLoadMap.value[scope.index]?.loading}
          v-model={scope.row.del}
          active-value={0}
          inactive-value={1}
          active-text="已开启"
          inactive-text="已关闭"
          inline-prompt
          style={switchStyle.value}
          onChange={() => onChange(scope as any)}
        />
      )
    }
  ];

  function onChange({ row, index }) {
    ElMessageBox.confirm(
      `确认要<strong>${
        row.del === 1 ? "停用" : "启用"
      }</strong><strong style='color:var(--el-color-primary)'>${
        row.title
      }</strong>菜单吗?`,
      "系统提示",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true,
        draggable: true
      }
    )
      .then(() => {
        switchLoadMap.value[index] = Object.assign(
          {},
          switchLoadMap.value[index],
          {
            loading: true
          }
        );
        updateStudentRouterStatus({
          id: row.id,
          del: row.del
        })
          .then(res => {
            if (res.success) {
              message("已成功修改菜单状态", { type: "success" });
              onSearch();
              switchLoadMap.value[index] = Object.assign(
                {},
                switchLoadMap.value[index],
                {
                  loading: false
                }
              );
            } else {
              row.del === 0 ? (row.del = 1) : (row.del = 0); //异常状态恢复
              message("修改状态异常:" + res.errMsg, { type: "error" });
            }
          })
          .catch(error => {
            row.del === 0 ? (row.del = 1) : (row.del = 0); //异常状态恢复
            message("修改状态异常:" + JSON.stringify(error), { type: "error" });
          });
      })
      .catch(() => {
        row.del === 0 ? (row.del = 1) : (row.del = 0); //状态恢复
      });
  }

  async function onSearch() {
    loading.value = true;
    queryStudentRouterList()
      .then(res => {
        console.log(JSON.stringify(res));
        if (res.success) {
          dataList.value = res.data.list;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    columns,
    dataList
  };
}
